package com.panshi.modules.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.panshi.modules.domain.SysMenu;
import com.panshi.modules.mapper.SysMenuMapper;
import com.panshi.modules.service.SysMenuService;
import com.panshi.vo.SelectVO;
import com.panshi.vo.SysMenuVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 857
 * @since 2020-07-29
 */
@Service
public class SysMenuServiceImpl  implements SysMenuService {

    @Autowired
    private SysMenuMapper sysMenuMapper;

    @Override
    public List<SysMenuVO> listSysMenuByRoleIds(List<Integer> roleIds) {
        List<SysMenu> sysMenus = sysMenuMapper.listSysMenuByRoleIds(roleIds);

        if (CollectionUtils.isEmpty(sysMenus)){
            return new ArrayList<>();
        }
        List<SysMenuVO> list = new ArrayList<>();
        for(SysMenu menu : sysMenus){
            SysMenuVO menuVO = new SysMenuVO();
            BeanUtils.copyProperties(menu , menuVO);
            list.add(menuVO);
        }
        return list;
    }

    /**
     * 分页查询
     * @param page
     * @return
     */
    public IPage<SysMenu> pageMenuList(IPage<SysMenu> page){
        return sysMenuMapper.selectPage(page, null);
    }

    /**
     * 树形菜单列表
     * @return
     */
    @Override
    public List<SysMenuVO> listSysMenu(){
        List<SysMenu> sysMenus = sysMenuMapper.selectList(null);
        List<SysMenuVO> list = new ArrayList<>();
        for(SysMenu menu : sysMenus){
            SysMenuVO menuVO = new SysMenuVO();
            BeanUtils.copyProperties(menu,menuVO);
            list.add(menuVO);
        }
        return list;
    }

    @Override
    //@Cacheable(value = "system:sysUser",key="#root.methodName")
    public List<SysMenuVO> listSysMenuTree() {

        List<SysMenuVO> sysMenus = this.listSysMenu();
        List<SysMenuVO> collect = sysMenus.stream()
                .filter(sysMenu -> sysMenu.getPid() == 0 ).collect(Collectors.toList());

        for(SysMenuVO menu : collect){
            menu.setChildren(getChildrens(menu,sysMenus));
        }

        return collect;
    }

    @Override
    public SysMenuVO getSysMenu(Integer id) {
        SysMenu menu = sysMenuMapper.selectById(id);
        SysMenuVO menuVO = new SysMenuVO();
        BeanUtils.copyProperties(menu , menuVO);
        return menuVO;
    }

    @Override
    public int addSysMenu(SysMenuVO sysMenuVO) {
        SysMenu menu = new SysMenu();
        BeanUtils.copyProperties(sysMenuVO,menu);
        int result = sysMenuMapper.insert(menu);
        return result;
    }

    @Override
    public int editSysMenu(SysMenuVO sysMenuVO) {
        SysMenu menu = new SysMenu();
        BeanUtils.copyProperties(sysMenuVO,menu);
        int result = sysMenuMapper.updateById(menu);
        return result;
    }

    @Override
    public int delSysMenu(Integer id) {
        int result = sysMenuMapper.deleteById(id);
        return result;
    }


    //获取所有权限列表给xmSelect
    public List<SelectVO> xmSelect() {
        List<SysMenuVO> menuVOList = this.listSysMenu();
        List<SelectVO> select = getSelect(menuVOList);
        return select;
    }

    //获取指定roles 的权限
    public List<SelectVO> getRoleXmselect(Integer id){
        List<SysMenuVO> menuVOList = this.listSysMenuByRoleIds(Arrays.asList(id));
        List<SelectVO> select = getSelect(menuVOList);
        return select;
    }

    private List<SelectVO> getSelect(List<SysMenuVO> menuVOList){
        List<SysMenuVO> collect = menuVOList.stream().filter(menu -> menu.getType() != 0).collect(Collectors.toList());
        List<SelectVO> collect1 = collect.stream().map(menu -> new SelectVO(menu.getMenuname(), menu.getId().toString())).collect(Collectors.toList());
        return collect1;
    }


    //递归查找子菜单
    public List<SysMenuVO> getChildrens(SysMenuVO root, List<SysMenuVO> all){
        List<SysMenuVO> children = all.stream()
                .filter(sysMenu -> sysMenu.getPid() == root.getId())
                .collect(Collectors.toList());
        System.out.println("children: {}"+children);
        return children;
    }


    //===============count
    @Override
    public int countMenuname(String menuname,Integer id) {
        return sysMenuMapper.selectCount(new QueryWrapper<SysMenu>().eq("menuname",menuname).ne("id",id));
    }

    @Override
    public int countPermission(String permission,Integer id) {
        return sysMenuMapper.selectCount(new QueryWrapper<SysMenu>().eq("permission",permission).ne("id",id));
    }


    //=======================================================================================================================================

    @Override
    public List<SysMenu> listMenu(){

        List<SysMenu> sysMenus = sysMenuMapper.selectList(new QueryWrapper<SysMenu>().eq("status",1));

        return sysMenus;
    }

    @Override
    public int countMenus() {
        Integer integer = sysMenuMapper.selectCount(null);

        return integer;
    }
}
